package com.gtt.jr.app;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.gtt.jr.app.domain.ContainerVO;
import com.gtt.jr.constants.Status;
import com.gtt.jr.repository.domain.Container;
import com.gtt.jr.repository.domain.ContainerTypeBridge;
import com.gtt.jr.service.ContainerService;
import com.gtt.jr.service.ContainerTypeBridgeService;
import com.gtt.jr.service.TypeService;

/**
 * 集装箱库实现
 * 
 * @author 高甜甜(gao12581@sina.com)
 * @date 2010-9-4
 * 
 */
@Service("containerRepository")
public class ContainerRepositoryImpl implements ContainerRepository {

	@Resource
	private ContainerService containerService;

	@Resource
	private ContainerTypeBridgeService containerTypeBridgeService;

	@Resource
	private TypeService typeService;

	public List<ContainerVO> find(Long typeId) throws Exception {
		List<Container> containers = containerService.findByType(typeId);

		List<ContainerVO> result = new ArrayList<ContainerVO>();
		for (Container container : containers) {
			result.add(convert(container, typeId));
		}
		return result;
	}

	public void add(ContainerVO containerVO) {
		Container container = new Container();
		container.setName(containerVO.getName());
		container.setDescription(containerVO.getDescription());
		container.setUrl(containerVO.getUrl());
		container.setSurl(containerVO.getSurl());
		container.setTurl(containerVO.getTurl());
		containerService.save(container);

		ContainerTypeBridge bridge = new ContainerTypeBridge();
		bridge.setContainer(container);
		bridge.setContainerType(typeService.get(containerVO.getType()));
		containerTypeBridgeService.save(bridge);
	}

	private ContainerVO convert(Container container, Long typeId) {
		ContainerVO vo = new ContainerVO();
		vo.setId(container.getId());
		vo.setName(container.getName());
		vo.setDescription(container.getDescription());
		vo.setSurl(container.getSurl());
		vo.setUrl(container.getUrl());
		vo.setTurl(container.getTurl());
		if (typeId != null) {
			vo.setType(typeId);
		} else {
			vo.setType(containerTypeBridgeService.get(container.getId())
					.getId());
		}
		return vo;
	}

	public void del(Long id) {
		Container container = containerService.load(id);
		container.setStatus(Status.DELETE);
		containerService.save(container);
	}

	public ContainerVO get(Long id) {
		Container container = containerService.load(id);
		return convert(container, null);
	}

	@Override
	public void modify(ContainerVO vo) {
		Container container = containerService.load(vo.getId());
		container.setName(vo.getName());
		container.setDescription(vo.getDescription());
		if (StringUtils.isNotEmpty(vo.getUrl())) {
			container.setUrl(vo.getUrl());
			container.setSurl(vo.getSurl());
			container.setTurl(vo.getTurl());
		}
		containerService.save(container);

		ContainerTypeBridge bridge = containerTypeBridgeService.get(vo.getId());
		if (bridge.getContainerType().getId().equals(vo.getType())) {
			return;
		}

		bridge.setContainerType(typeService.get(vo.getType()));
		containerTypeBridgeService.save(bridge);

	}
}
